package pzl.com.bluetooth.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import pzl.com.bluetooth.R;

/**
 * 服务端接收数据的适配器
 *
 * @author zl.peng
 * @version [1.0, 2016-10-11]
 */

public class ReceiveDataAdapter extends BaseAdapter {
    List<Map<String, Object>> data = new ArrayList();
    LayoutInflater layoutInflater;
    Context mContext;

    public ReceiveDataAdapter(Context context) {
        mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_receivedata, null);
        }
        TextView tvNick = (TextView) convertView.findViewById(R.id.tv_nick);
        TextView tvMsg = (TextView) convertView.findViewById(R.id.tv_msg);
        ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
        Map<String, Object> item = data.get(position);

        tvNick.setText((String) item.get("nick"));

        switch ((int) item.get("identity")) {
            case 0:
                tvNick.setTextColor(Color.parseColor("#e1cf34"));
                break;
            case 1:
                tvNick.setTextColor(Color.parseColor("#cf50e2"));
                break;
        }

        tvMsg.setVisibility(View.GONE);
        iv.setVisibility(View.GONE);
        if (item.containsKey("text")) {
            String text = (String) item.get("text");
            tvMsg.setVisibility(View.VISIBLE);
            tvMsg.setText(text);
        } else {
            tvMsg.setVisibility(View.GONE);
        }

        if (item.containsKey("img")) {
            Bitmap bitmap = (Bitmap) item.get("img");
            iv.setVisibility(View.VISIBLE);
            iv.setImageBitmap(bitmap);
        } else {
            iv.setVisibility(View.GONE);
        }


        return convertView;
    }

    public void addItem(Map<String, Object> item) {
        data.add(item);
    }
}
